//
//  PaymentViewController.m
//  Accounting
//
//  Created by Billy on 15/9/1.
//  Copyright (c) 2015年 hzqs. All rights reserved.
//

#import "PaymentViewController.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

@interface PaymentViewController ()

@end

@implementation PaymentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self.navigationController setNavigationBarHidden:NO];
    
    NSString *sessionId = [[NSUserDefaults standardUserDefaults] valueForKey:@"sessionId"];
    
    NSLog(@"payment page got sessioinid :%@",sessionId);
    [self pageSettingsAndSubviewsCreation];
    //添加支付页面的UI，按钮调用 payment 方法
    
    
    
    
    
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(11, 8, SCREEN_WIDTH-22, 100)];
    image.image = [UIImage imageNamed:@"支付-优惠框@2x.png.png"];
    //    image.backgroundColor = [UIColor redColor];
    [self.view addSubview:image];
    
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    lab.text = @"300.00元";
    //    @"300.00元";
    lab.textAlignment = NSTextAlignmentCenter;
    lab.font = [UIFont systemFontOfSize:32];
    lab.textColor = [UIColor orangeColor];
    [image addSubview:lab];
    
    
    
    UIImageView * imagedow = [[UIImageView alloc]initWithFrame:CGRectMake(0,  image.frame.origin.y+image.frame.size.height+10, SCREEN_WIDTH, 150)];
    imagedow.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:imagedow];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 8+330, SCREEN_WIDTH, 1)];
    lineView.backgroundColor = UIColorWithRGBA(215, 215, 215, 1);
    [self.view addSubview:lineView];
    
    UIImageView * imagebigDown = [[UIImageView alloc]initWithFrame:CGRectMake(0,imagedow.frame.origin.y+imagedow.frame.size.height+5, SCREEN_WIDTH, 84)];
    imagebigDown.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:imagebigDown];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(-10, SCREEN_HEIGHT-50-NavbarHight, SCREEN_WIDTH+20, 50);
    [btn addTarget:self action:@selector(payment) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:@"确认支付@2x.png.png"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"确认支付-选中@2x.png.png"] forState:UIControlStateHighlighted];
    [self.view addSubview:btn];
    
    
    UILabel * lab1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH, 40)];
    lab1.text = @"lalala";
    lab1.textAlignment = NSTextAlignmentCenter;
    lab1.font = [UIFont systemFontOfSize:20];
    [imagedow addSubview:lab1];
    
    //
    //    UILabel * lab2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, SCREEN_WIDTH/2-30, 30)];
    //    lab2.text = @"详细信息";
    //    lab2.textAlignment = NSTextAlignmentCenter;
    //    lab2.font = [UIFont systemFontOfSize:15];
    //    lab2.textColor = [UIColor grayColor];
    //    [imagedow addSubview:lab2];
    
    
    UILabel * lab3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, SCREEN_WIDTH/2, 30)];
    lab3.text = @"支付方式:";
    lab3.textAlignment = NSTextAlignmentLeft;
    lab3.font = [UIFont systemFontOfSize:20];
    [imagebigDown addSubview:lab3];
    
    UIButton * payBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 40, 80, 32)];
    [payBtn setBackgroundImage:[UIImage imageNamed:@"支付宝@2x.png.png"] forState:UIControlStateNormal];
    [imagebigDown addSubview:payBtn];

    
    
    
    
    
}
-(void)yesBtn{

    
    


}

#pragma mark - SubviewsCreationAndPageSettings

- (void) pageSettingsAndSubviewsCreation {
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) payment {
    
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *seller = SellerID;
    
//    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString *privateKey = PartnerPrivKey;
    
//    NSUserDefaults *user2 = [NSUserDefaults standardUserDefaults];
    NSString *partner = PartnerID;
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    
    order.tradeNO = @"7758" ;//订单ID（由商家自行制定）
    order.productName =  @"是我没错了" ;  //商品标题
    order.productDescription = @"就是这个" ; //商品描述
    
    [order setAmount:[NSString stringWithFormat:@"%.2f",0.01]];
    //    order.amount = self.number; //商品价格
    order.notifyURL =  @"http://www.maimiao.com"; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"hzqs";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    //NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            
            if ([[resultDic objectForKey:@"resultStatus"] isEqualToString:@"9000"]) {

                NSLog(@"成功的");
                //成功的回调
            }else  {
                NSLog(@"失败的");

                //失败的回调
            }
        }];
    }
}


@end
